
package com.lzx.adx.report.controller.v1.proxy;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.lzs.adx.commmon.utils.JsonUtils;
import com.lzs.adx.commmon.utils.Result;
import com.lzx.adx.report.bean.mapper.ReportMapper;
import com.lzx.adx.report.cache.AccountLocalCache;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.model.common.CommonReportRequest;
import com.lzx.adx.report.model.hb.HaiBeiRequest;
import com.lzx.adx.report.model.proxy.ProxyReq;
import com.lzx.adx.report.model.youyou.YouYouRequest;
import com.lzx.adx.report.service.ReportService;
import com.lzx.adx.report.util.AdPerUtil;
import com.lzx.adx.report.util.ResponseVO;
import com.lzx.adx.report.util.URLEncoderDecoderTool;
import com.lzx.adx.report.util.helper.KkHeper;
import com.lzx.adx.report.util.helper.ProxyHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * 通用代理媒体类
 */
@RequestMapping("/api/proxy")
@RestController
@Slf4j
public class ProxyReportController {


    @Autowired
    private ReportService reportService;

    @Autowired
    private ThreadPoolTaskExecutor reportExecutor;


    @GetMapping("/report")
    public ResponseVO report(ProxyReq proxyReq) {
        try {
            log.info("账号为{}接收到代理媒体传来的请求参数为{}", proxyReq.getDspId(), JsonUtils.toJsonString(proxyReq));
            proxyReq.checkParam();
            //数据存储
            ReportDTO reportDTO = new ReportDTO();
            BeanUtils.copyProperties(proxyReq, reportDTO);
            reportDTO.setBizRequestId(IdUtil.fastSimpleUUID()+reportDTO.getDspId());
            reportDTO.setCreateTime(new Date());
            reportDTO.setUpdateTime(new Date());
            reportDTO.setExtend(JsonUtils.toJsonString(proxyReq));
            reportDTO.setUser_agent(proxyReq.getUserAgent());
            reportDTO.setHash_android_id(proxyReq.getHashAndroidId());
            reportExecutor.execute(() -> {
                reportService.saveData(reportDTO);
            });
            AccountDTO accountDTO = AccountLocalCache.getInstance().get(proxyReq.getDspId());
            String finalUrl = ProxyHelper.handUrl(reportDTO, accountDTO);
            log.info("账号为{},完整的{}请求地址为{}", proxyReq.getDspId(), accountDTO.getName(), finalUrl);
            //上报接口
            String body = HttpUtil.createGet(finalUrl).execute().body();
            log.info("账号为{} 完整的{}响应的结果为{}", proxyReq.getDspId(), accountDTO.getName(), body);
            return ResponseVO.success();
        } catch (IllegalArgumentException e) {
            log.error("接收到代理媒体传来的请求参数异常", e);
            return ResponseVO.error("1001", e.getMessage());
        } catch (Exception e) {
            log.error("接收到代理媒体传来请求发生异常.", e);
            return ResponseVO.error("9999", "服务器异常");
        }

    }


    /**
     * 夸克回传
     *
     * @param requestId 请求参数
     * @return ResponseEntity
     * @throws Exception
     */
    @GetMapping("/szdb/kk/callback")
    public ResponseEntity kkCallBackUrl(String requestId) throws Exception {
        String eventType = "1";
        log.info("接收到夸克回传到深圳大白的请求Id:{}", requestId);
        if (StringUtils.isBlank(requestId)) {
            log.info("接收到夸克回传到深圳大白的请求Id空");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", requestId);
        List<ReportDTO> list = reportService.list(params);
        String time = String.valueOf(System.currentTimeMillis());
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);
            reportDTO.setResultType("1");
            reportDTO.setResultTime(time);
            reportDTO.setUpdateTime(new Date());
            reportService.updateData(reportDTO,eventType);
        } else {
            log.error("夸克回传深圳大白流水号{}，系统当前不存在", requestId);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        //把数据回传给深圳大白
        String callback = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        log.info("dspId{},请求流水号{}夸克回传给深圳大白的地址为：{}", reportDTO.getDspId(), requestId, callback);
        if (!callback.contains("conv_action=__EVENT__")) {
            log.info("dspId{},请求流水号{}夸克回传给深圳大白的地址不包含转化回传信息conv_action=__EVENT__ 不做回传", reportDTO.getDspId(), requestId);
            return ResponseEntity.ok(new Result());
        }
        String replaceCallback = callback.replace("__EVENT__", "1");
        log.info("dspId{},请求流水号{}夸克回传给深圳大白的最终地址为：{}", reportDTO.getDspId(), requestId, replaceCallback);
        String result = HttpUtil.get(replaceCallback);
        log.info("dspId{},请求流水号{}夸克回传给深圳大白的响应结果：{}", reportDTO.getDspId(), requestId, result);
        return ResponseEntity.ok(new Result());
    }








    /**
     * 夸克回传
     *
     * @param requestId 请求参数
     * @return ResponseEntity
     * @throws Exception
     */
    @GetMapping("/xy/kk/callback")
    public ResponseEntity yxKkCallBackUrl(String requestId) throws Exception {
        String eventType = "1";
        log.info("接收到夸克回传到炫逸的请求Id:{}", requestId);
        if (StringUtils.isBlank(requestId)) {
            log.info("接收到夸克回传到炫逸的请求Id空");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", requestId);
        List<ReportDTO> list = reportService.list(params);
        String time = String.valueOf(System.currentTimeMillis());
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);
            reportDTO.setResultType(eventType);
            reportDTO.setResultTime(time);
            reportDTO.setUpdateTime(new Date());
            reportService.updateData(reportDTO,eventType);
        } else {
            log.error("夸克回传炫逸流水号{}，系统当前不存在", requestId);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        //把数据回传给深圳大白
        String callback = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        log.info("dspId{},请求流水号{}夸克回传给炫逸的地址为：{}", reportDTO.getDspId(), requestId, callback);
        if (!callback.contains("conv_action=__EVENT__")) {
            log.info("dspId{},请求流水号{}夸克回传给深圳大白的地址不包含转化回传信息conv_action=__EVENT__ 不做回传", reportDTO.getDspId(), requestId);
            return ResponseEntity.ok(new Result());
        }
        String replaceCallback = callback.replace("__EVENT__", "1");
        log.info("dspId{},请求流水号{}夸克回传给炫逸的最终地址为：{}", reportDTO.getDspId(), requestId, replaceCallback);
        String result = HttpUtil.get(replaceCallback);
        log.info("dspId{},请求流水号{}夸克回传给炫逸的响应结果：{}", reportDTO.getDspId(), requestId, result);
        return ResponseEntity.ok(new Result());
    }



    /**
     * 夸克回传
     *
     * @param requestId 请求参数
     * @return ResponseEntity
     * @throws Exception
     */
    @GetMapping("/xy/yty/callback")
    public ResponseEntity xyYtyCallBackUrl(String requestId,String event_type) throws Exception {
        log.info("接收到炫逸回传到因特云的请求Id:{}", requestId);
        if (StringUtils.isBlank(requestId)) {
            log.info("接收到炫逸回传到因特云的请求Id空");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", requestId);
        List<ReportDTO> list = reportService.list(params);
        String time = String.valueOf(System.currentTimeMillis());
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);

        } else {
            log.error("炫逸回传因特云流水号{}，系统当前不存在", requestId);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }

        String callback = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        log.info("dspId{},请求流水号{}炫逸回传给因特云的地址为：{}", reportDTO.getDspId(), requestId, callback);
        if (!callback.contains("conv_action=__EVENT__")) {
            log.info("dspId{},请求流水号{}炫逸回传给因特云地址不包含转化回传信息conv_action=__EVENT__ 不做回传", reportDTO.getDspId(), requestId);
            return ResponseEntity.ok(new Result());
        }

        AccountDTO accountDTO = AccountLocalCache.getInstance().get(reportDTO.getDspId());
        String sspExtend = accountDTO.getSspExtend();
        JSONObject jsonObject = JSONObject.parseObject(sspExtend);
        String transformType = jsonObject.getString(event_type);
        if (StringUtils.isBlank(transformType)){
            log.info("收到炫逸的回调信息当前流水号：{}，事件类型{}不合法，不做数据回传", requestId,event_type);
            return ResponseEntity.ok(new Result());
        }

        reportDTO.setResultType(event_type);
        reportDTO.setResultTime(time);
        reportDTO.setUpdateTime(new Date());
        reportService.updateData(reportDTO,event_type);

        Double per = jsonObject.getDouble("per");
        if (Objects.nonNull(per)) {
            if(!AdPerUtil.randomPer(per)) {
                log.info("账户{}流水号{}超出了比例,不做回传", reportDTO.getDspId(), reportDTO.getBizRequestId());
                return ResponseEntity.ok(new Result());
            }
        }


        String replaceCallback = callback.replace("__EVENT__", transformType);
        log.info("dspId{},请求流水号{}炫逸回传给因特云的最终地址为：{}", reportDTO.getDspId(), requestId, replaceCallback);
        String result = HttpUtil.get(replaceCallback);
        log.info("dspId{},请求流水号{}炫逸回传给因特云的响应结果：{}", reportDTO.getDspId(), requestId, result);
        return ResponseEntity.ok(new Result());
    }




    /**
     * 夸克回传
     *
     * @param requestId 请求参数
     * @return ResponseEntity
     * @throws Exception
     */
    @GetMapping("/yty/kk/callback")
    public ResponseEntity ytyKkCallBackUrl(String requestId) throws Exception {
        String eventType = "1";
        log.info("接收到夸克回传到因特云的请求Id:{}", requestId);
        if (StringUtils.isBlank(requestId)) {
            log.info("接收到夸克回传到因特云的请求Id空");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", requestId);
        List<ReportDTO> list = reportService.list(params);
        String time = String.valueOf(System.currentTimeMillis());
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);
            reportDTO.setResultType(eventType);
            reportDTO.setResultTime(time);
            reportDTO.setUpdateTime(new Date());
            reportService.updateData(reportDTO,eventType);
        } else {
            log.error("夸克回传因特云流水号{}，系统当前不存在", requestId);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }

        String callback = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        log.info("dspId{},请求流水号{}夸克回传给因特云的地址为：{}", reportDTO.getDspId(), requestId, callback);
        if (!callback.contains("conv_action=__EVENT__")) {
            log.info("dspId{},请求流水号{}夸克回传给深圳大白的地址不包含转化回传信息conv_action=__EVENT__ 不做回传", reportDTO.getDspId(), requestId);
            return ResponseEntity.ok(new Result());
        }
        String replaceCallback = callback.replace("__EVENT__", "1");
        log.info("dspId{},请求流水号{}夸克回传给因特云的最终地址为：{}", reportDTO.getDspId(), requestId, replaceCallback);
        String result = HttpUtil.get(replaceCallback);
        log.info("dspId{},请求流水号{}夸克回传给因特云的响应结果：{}", reportDTO.getDspId(), requestId, result);
        return ResponseEntity.ok(new Result());
    }



    /**
     * 京东金融回传
     *
     * @param requestId 请求参数
     * @return ResponseEntity
     * @throws Exception
     */
    @GetMapping("/jdjr/callback")
    public ResponseEntity jdjrCallBackUrl(String requestId, String click_id, String conv_type, String conv_ts) {
        log.info("接收了京东金融回调给的回调信息，请求流水号{},回调类型为{}，回调时间{}", click_id, conv_type, conv_ts);
        if (StringUtils.isBlank(conv_type) || StringUtils.isBlank(click_id)) {
            log.info("接收了京东金融回调的回调信息请求参数异常，,click_id{} event_type{},event_time{}", click_id, conv_type, conv_ts);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }


        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", requestId);
        List<ReportDTO> list = reportService.list(params);
        String time = String.valueOf(System.currentTimeMillis());
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);
            reportService.updateData(reportDTO,conv_type);
        } else {
            log.error("京东金融回传流水号{}，系统当前不存在", requestId);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }

        AccountDTO accountDTO = AccountLocalCache.getInstance().get(reportDTO.getDspId());
        String sspExtend = accountDTO.getSspExtend();
        JSONObject jsonObject = JSONObject.parseObject(sspExtend);
        String code = jsonObject.getString(conv_type);
        if (StringUtils.isBlank(code)){
            log.info("接收了京东金融回调的回调信息当前流水号：{}，事件类型{}不合法，不做数据回传", requestId,conv_type);
            return ResponseEntity.ok(new Result());
        }

        //把数据回传给深圳大白
        String callback = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        log.info("dspId{},请求流水号{}京东金融回传的地址为：{}", reportDTO.getDspId(), requestId, callback);
        if (!callback.contains("conv_action=__EVENT__")) {
            log.info("dspId{},请求流水号{}京东金融回传的地址不包含转化回传信息conv_action=__EVENT__ 不做回传", reportDTO.getDspId(), requestId);
            return ResponseEntity.ok(new Result());
        }
        String replaceCallback = callback.replace("__EVENT__", code);
        log.info("dspId{},请求流水号{}京东金融回传的最终地址为：{}", reportDTO.getDspId(), requestId, replaceCallback);
        String result = HttpUtil.get(replaceCallback);
        log.info("dspId{},请求流水号{}京东金融回传白的响应结果：{}", reportDTO.getDspId(), requestId, result);
        return ResponseEntity.ok(new Result());
    }












    /**
     * YY直播-炫逸给西安小鱼
     *
     * @param requestId 请求参数
     * @return ResponseEntity
     * @throws Exception
     */
    @GetMapping("/xnxy/yyzb/callback")
    public ResponseEntity xnxyYyzbCallBackUrl(String requestId, String event_type) {
        log.info("接收了YY直播-炫逸给西安小鱼的回调信息请求流水号{},回调类型为{}", requestId, event_type);
        if (StringUtils.isBlank(requestId) || StringUtils.isBlank(event_type)) {
            log.info("接收了YY直播-炫逸给西安小鱼的回调信息请求参数异常,requestId{} event_type{}", requestId, event_type);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", requestId);
        List<ReportDTO> list = reportService.list(params);
        String time = String.valueOf(System.currentTimeMillis());
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);
        } else {
            log.error("YY直播-炫逸给西安小鱼回传流水号{}，系统当前不存在", requestId);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        AccountDTO accountDTO = AccountLocalCache.getInstance().get(reportDTO.getDspId());
        String sspExtend = accountDTO.getSspExtend();
        JSONObject jsonObject = JSONObject.parseObject(sspExtend);
        String transformType = jsonObject.getString(event_type);
        if (StringUtils.isBlank(transformType)){
            log.info("接收了YY直播-炫逸给西安小鱼的回调信息当前流水号：{}，事件类型{}不合法，不做数据回传", requestId,event_type);
            return ResponseEntity.ok(new Result());
        }
        reportDTO.setResultType(event_type);
        reportDTO.setResultTime(time);
        reportDTO.setUpdateTime(new Date());
        reportService.updateData(reportDTO,event_type);
        //把数据回传给深圳大白
        String callback = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        log.info("dspId{},请求流水号{}YY直播-炫逸回传给西安小鱼的地址为：{}", reportDTO.getDspId(), requestId, callback);
        if (!callback.contains("conv_action=__EVENT__")) {
            log.info("dspId{},请求流水号{}YY直播-炫逸回传给西安小鱼的地址不包含转化回传信息conv_action=__EVENT__ 不做回传", reportDTO.getDspId(), requestId);
            return ResponseEntity.ok(new Result());
        }
        String replaceCallback = callback.replace("__EVENT__", transformType);
        log.info("dspId{},请求流水号{}YY直播-炫逸回传给西安小鱼的最终地址为：{}", reportDTO.getDspId(), requestId, replaceCallback);
        String result = HttpUtil.get(replaceCallback);
        log.info("dspId{},请求流水号{}YY直播-炫逸回传给西安小鱼的响应结果：{}", reportDTO.getDspId(), requestId, result);
        return ResponseEntity.ok(new Result());
    }



    /**
     * YY直播-创智 给西安小鱼
     *
     * @param requestId 请求参数
     * @return ResponseEntity
     * @throws Exception
     */
    @GetMapping("/xnxy/czyy/callback")
    public ResponseEntity xnxyCzyyCallBackUrl(String requestId, String tnaEvent) {
        log.info("接收了创智的yy直播的回调信息请求流水号{},回调类型为{}", requestId, tnaEvent);
        if (StringUtils.isBlank(requestId) ) {
            log.info("接收了西安小鱼-创智的yy直播的回调信息请求参数异常,requestId{} event_type{}", requestId, tnaEvent);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }

        //回传为空就代表激活
        if(StringUtils.isBlank(tnaEvent)){
            tnaEvent="active";
        }


        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", requestId);
        List<ReportDTO> list = reportService.list(params);
        String time = String.valueOf(System.currentTimeMillis());
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);
        } else {
            log.error("创智的yy直播回传流水号{}，系统当前不存在", requestId);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        AccountDTO accountDTO = AccountLocalCache.getInstance().get(reportDTO.getDspId());
        String sspExtend = accountDTO.getSspExtend();
        JSONObject jsonObject = JSONObject.parseObject(sspExtend);
        String code = jsonObject.getString(tnaEvent);
        if (StringUtils.isBlank(code)){
            log.info("接收了创智的yy直播给西安小鱼的回调信息当前流水号：{}，事件类型{}不合法，不做数据回传", requestId,tnaEvent);
            return ResponseEntity.ok(new Result());
        }

        reportDTO.setResultType(code);
        reportDTO.setResultTime(time);
        reportDTO.setUpdateTime(new Date());
        reportService.updateData(reportDTO,tnaEvent);
        //把数据回传给深圳大白
        String callback = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        log.info("dspId{},请求流水号{}YY直播-创智回传给西安小鱼的地址为：{}", reportDTO.getDspId(), requestId, callback);
        if (!callback.contains("conv_action=__EVENT__")) {
            log.info("dspId{},请求流水号{}YY直播-创智回传给西安小鱼的地址不包含转化回传信息conv_action=__EVENT__ 不做回传", reportDTO.getDspId(), requestId);
            return ResponseEntity.ok(new Result());
        }
        String replaceCallback = callback.replace("__EVENT__", code);
        log.info("dspId{},请求流水号{}YY直播-创智回传给西安小鱼的最终地址为：{}", reportDTO.getDspId(), requestId, replaceCallback);
        String result = HttpUtil.get(replaceCallback);
        log.info("dspId{},请求流水号{}YY直播-创智回传给西安小鱼的响应结果：{}", reportDTO.getDspId(), requestId, result);
        return ResponseEntity.ok(new Result());
    }







    /**
     * YY直播-炫逸给西安小鱼
     *
     * @param requestId 请求参数
     * @return ResponseEntity
     * @throws Exception
     */
    @GetMapping("/xnxy/aq/callback")
    public ResponseEntity xnxyAqCallBackUrl(String requestId, String event_type) {
        log.info("接收了爱聊-炫逸给西安小鱼的回调信息请求流水号{},回调类型为{}", requestId, event_type);
        if (StringUtils.isBlank(requestId) || StringUtils.isBlank(event_type)) {
            log.info("接收了爱聊-炫逸给西安小鱼的回调信息请求参数异常,requestId{} event_type{}", requestId, event_type);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", requestId);
        List<ReportDTO> list = reportService.list(params);
        String time = String.valueOf(System.currentTimeMillis());
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);
        } else {
            log.error("爱聊-炫逸给西安小鱼回传流水号{}，系统当前不存在", requestId);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        AccountDTO accountDTO = AccountLocalCache.getInstance().get(reportDTO.getDspId());
        String sspExtend = accountDTO.getSspExtend();
        JSONObject jsonObject = JSONObject.parseObject(sspExtend);
        String transformType = jsonObject.getString(event_type);
        if (StringUtils.isBlank(transformType)){
            log.info("接收了Y爱聊-炫逸给西安小鱼的回调信息当前流水号：{}，事件类型{}不合法，不做数据回传", requestId,event_type);
            return ResponseEntity.ok(new Result());
        }
        reportDTO.setResultType(event_type);
        reportDTO.setResultTime(time);
        reportDTO.setUpdateTime(new Date());
        reportService.updateData(reportDTO,event_type);
        //把数据回传给深圳大白
        String callback = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        log.info("dspId{},请求流水号{}爱聊-炫逸回传给西安小鱼的地址为：{}", reportDTO.getDspId(), requestId, callback);
        if (!callback.contains("conv_action=__EVENT__")) {
            log.info("dspId{},请求流水号{}爱聊-炫逸回传给西安小鱼的地址不包含转化回传信息conv_action=__EVENT__ 不做回传", reportDTO.getDspId(), requestId);
            return ResponseEntity.ok(new Result());
        }
        String replaceCallback = callback.replace("__EVENT__", transformType);
        log.info("dspId{},请求流水号{}爱聊-炫逸回传给西安小鱼的最终地址为：{}", reportDTO.getDspId(), requestId, replaceCallback);
        String result = HttpUtil.get(replaceCallback);
        log.info("dspId{},请求流水号{}爱聊-炫逸回传给西安小鱼的响应结果：{}", reportDTO.getDspId(), requestId, result);
        return ResponseEntity.ok(new Result());
    }




    @GetMapping("/cz/tt/callback")
    public ResponseEntity czTtCallBackUrl(String requestId,String tnaEvent) {
        log.info("接收了创智的tt语音的回调信息请求流水号{},回调类型为{}", requestId, tnaEvent);
        if (StringUtils.isBlank(requestId)) {
            log.info("接收了番茄小说给金袋的回调信息请求参数异常,requestId{} event_type{}", requestId, tnaEvent);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", requestId);
        List<ReportDTO> list = reportService.list(params);
        String time = String.valueOf(System.currentTimeMillis());
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);
        } else {
            log.error("创智的tt语音回传流水号{}，系统当前不存在", requestId);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        AccountDTO accountDTO = AccountLocalCache.getInstance().get(reportDTO.getDspId());
        String sspExtend = accountDTO.getSspExtend();
        JSONObject jsonObject = JSONObject.parseObject(sspExtend);
        String code = jsonObject.getString(tnaEvent);
        if (StringUtils.isBlank(code)){
            log.info("接收了创智的tt语音给新量象的回调信息当前流水号：{}，事件类型{}不合法，不做数据回传", requestId,tnaEvent);
            return ResponseEntity.ok(new Result());
        }

        reportDTO.setResultType(code);
        reportDTO.setResultTime(time);
        reportDTO.setUpdateTime(new Date());
        reportService.updateData(reportDTO,tnaEvent);
        String callback = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        log.info("dspId{},请求流水号{}创智的tt语音回传给新量象的地址为：{}", reportDTO.getDspId(), requestId, callback);
        if (!callback.contains("conv_action=__EVENT__")) {
            log.info("dspId{},请求流水号{}创智的tt语音回传给新量象的地址不包含转化回传信息conv_action=__EVENT__ 不做回传", reportDTO.getDspId(), requestId);
            return ResponseEntity.ok(new Result());
        }
        String replaceCallback = callback.replace("__EVENT__", code);
        log.info("dspId{},请求流水号{}创智的tt语音回传给新量象的最终地址为：{}", reportDTO.getDspId(), requestId, replaceCallback);
        String result = HttpUtil.get(replaceCallback);
        log.info("dspId{},请求流水号{}创智的tt语音回传给新量象的响应结果：{}", reportDTO.getDspId(), requestId, result);
        return ResponseEntity.ok(new Result());
    }



    /**
     * 金易木-soul产品
     *
     * @return ResponseEntity
     * @throws Exception
     */
    @GetMapping("/jym/soul/callback")
    public ResponseEntity hzCallBackUrl(String clickId) throws Exception {

        String event_type = "1";
        log.info("夜雨网络收到金易木-soul的回调信息,请求流水号{}", clickId);
        if (StringUtils.isBlank(clickId)) {
            log.info("夜雨网络收到金易木-soul的回调信息,回调参数异常");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        //1.把转换结果更新到数据库
        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", clickId);
        List<ReportDTO> list = reportService.list(params);
        long currentTimeMillis = System.currentTimeMillis();
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);
        } else {
            log.error("当前流水号{}，系统当前不存在", clickId);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        String time =String.valueOf(System.currentTimeMillis());

        AccountDTO accountDTO = AccountLocalCache.getInstance().get(reportDTO.getDspId());
        String sspExtend = accountDTO.getSspExtend();
        JSONObject jsonObject = JSONObject.parseObject(sspExtend);
        String code ="activate";
        if (StringUtils.isBlank(code)){
            log.info("夜雨网络收到金易木-soul的回调信息当前流水号：{}，事件类型{}不合法，不做数据回传", clickId,code);
            return ResponseEntity.ok(new Result());
        }
        reportDTO.setResultType(code);
        reportDTO.setResultTime(time);
        reportDTO.setUpdateTime(new Date());
        reportService.updateData(reportDTO,event_type);
        //把数据回传给深圳大白
        String callback = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        log.info("dspId{},请求流水号{}番夜雨网络收到金易木-soul的地址为：{}", reportDTO.getDspId(), clickId, callback);
        if (!callback.contains("__EVENT__")) {
            log.info("dspId{},请求流水号{}夜雨网络收到金易木-soul的地址不包含转化回传信息__EVENT__ 不做回传", reportDTO.getDspId(), clickId);
            return ResponseEntity.ok(new Result());
        }
        String replaceCallback = callback.replace("__EVENT__", code);
        log.info("dspId{},请求流水号{}夜雨网络收到金易木-soul的的最终地址为：{}", reportDTO.getDspId(), clickId, replaceCallback);
        String result = HttpUtil.get(replaceCallback);
        log.info("dspId{},请求流水号{}夜雨网络收到金易木-soul的响应结果：{}", reportDTO.getDspId(), clickId, result);
        return ResponseEntity.ok(new Result());
    }






    @GetMapping("/jd/fqxs/callback")
    public ResponseEntity jdFqxsCallBackUrl(String requestId, String transformType) {
        log.info("接收了番茄小说给金袋的回调信息请求流水号{},回调类型为{}", requestId, transformType);
        if (StringUtils.isBlank(requestId) || StringUtils.isBlank(transformType)) {
            log.info("接收了番茄小说给金袋的回调信息请求参数异常,requestId{} event_type{}", requestId, transformType);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", requestId);
        List<ReportDTO> list = reportService.list(params);
        String time = String.valueOf(System.currentTimeMillis());
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);
        } else {
            log.error("番茄小说给金袋回传流水号{}，系统当前不存在", requestId);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        AccountDTO accountDTO = AccountLocalCache.getInstance().get(reportDTO.getDspId());
        String sspExtend = accountDTO.getSspExtend();
        JSONObject jsonObject = JSONObject.parseObject(sspExtend);
        String code = jsonObject.getString(transformType);
        if (StringUtils.isBlank(code)){
            log.info("接收了番茄小说给金袋的回调信息当前流水号：{}，事件类型{}不合法，不做数据回传", requestId,transformType);
            return ResponseEntity.ok(new Result());
        }
        reportDTO.setResultType(code);
        reportDTO.setResultTime(time);
        reportDTO.setUpdateTime(new Date());
        reportService.updateData(reportDTO,transformType);
        //把数据回传给深圳大白
        String callback = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        log.info("dspId{},请求流水号{}番茄小说回传给金袋的地址为：{}", reportDTO.getDspId(), requestId, callback);
        if (!callback.contains("conv_action=__EVENT__")) {
            log.info("dspId{},请求流水号{}番茄小说回传给金袋的地址不包含转化回传信息conv_action=__EVENT__ 不做回传", reportDTO.getDspId(), requestId);
            return ResponseEntity.ok(new Result());
        }
        String replaceCallback = callback.replace("__EVENT__", code);
        log.info("dspId{},请求流水号{}番茄小说回传给金袋的最终地址为：{}", reportDTO.getDspId(), requestId, replaceCallback);
        String result = HttpUtil.get(replaceCallback);
        log.info("dspId{},请求流水号{}番茄小说回传给金袋的响应结果：{}", reportDTO.getDspId(), requestId, result);
        return ResponseEntity.ok(new Result());
    }



    /**
     * @param requestId 请求参数
     * @return ResponseEntity
     * @throws Exception
     */
    @GetMapping("/yeyu/faq/callback")
    public ResponseEntity blblCallBackUrl(String requestId, String action_type) throws Exception {
        log.info("收到夜雨的回调信息，请求流水号{},回调类型为{}", requestId, action_type);
        if (StringUtils.isBlank(requestId)||StringUtils.isBlank(action_type)) {
            log.info("wifi收到哔哩哔哩回调信息参数异常");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        //1.把转换结果更新到数据库
        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", requestId);
        List<ReportDTO> list = reportService.list(params);
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);
        } else {
            log.error("当前流水号，系统当前不存在");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }

        String time = String.valueOf(System.currentTimeMillis()) ;

        AccountDTO accountDTO = AccountLocalCache.getInstance().get(reportDTO.getDspId());
        String sspExtend = accountDTO.getSspExtend();
        JSONObject jsonObject = JSONObject.parseObject(sspExtend);
        String code = jsonObject.getString(action_type);
        if (StringUtils.isBlank(code)){
            log.info("接收了番茄小说给金袋的回调信息当前流水号：{}，事件类型{}不合法，不做数据回传", requestId,action_type);
            return ResponseEntity.ok(new Result());
        }
        reportDTO.setResultType(action_type);
        reportDTO.setResultTime(time);
        reportDTO.setUpdateTime(new Date());
        reportService.updateData(reportDTO,action_type);
        //把数据回传给金袋
        String callback = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        log.info("dspId{},请求流水号{}夜雨番茄小说回传给金袋的地址为：{}", reportDTO.getDspId(), requestId, callback);
        if (!callback.contains("conv_action=__EVENT__")) {
            log.info("dspId{},请求流水号{}夜雨番茄小说回传给金袋的地址不包含转化回传信息conv_action=__EVENT__ 不做回传", reportDTO.getDspId(), requestId);
            return ResponseEntity.ok(new Result());
        }
        String replaceCallback = callback.replace("__EVENT__", code);
        log.info("dspId{},请求流水号{}夜雨番茄小说回传给金袋的最终地址为：{}", reportDTO.getDspId(), requestId, replaceCallback);
        String result = HttpUtil.get(replaceCallback);
        log.info("dspId{},请求流水号{}夜雨番茄小说回传给金袋的响应结果：{}", reportDTO.getDspId(), requestId, result);
        return ResponseEntity.ok(new Result());
    }



    /**
     * 夜雨通用
     * @param requestId 请求参数
     * @return ResponseEntity
     * @throws Exception
     */
    @GetMapping("/yeyu/callback")
    public ResponseEntity yeyuCallBackUrl(String requestId, String action_type) throws Exception {
        log.info("收到夜雨的回调信息，请求流水号{},回调类型为{}", requestId, action_type);
        if (StringUtils.isBlank(requestId)||StringUtils.isBlank(action_type)) {
            log.info("yeyuJuGaoCallBackUrl收到夜雨的回调信息回调信息参数异常");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        //1.把转换结果更新到数据库
        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", requestId);
        List<ReportDTO> list = reportService.list(params);
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);
        } else {
            log.error("当前流水号，系统当前不存在");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }

        String time = String.valueOf(System.currentTimeMillis()) ;

        AccountDTO accountDTO = AccountLocalCache.getInstance().get(reportDTO.getDspId());
        String sspExtend = accountDTO.getSspExtend();
        JSONObject jsonObject = JSONObject.parseObject(sspExtend);
        String code = jsonObject.getString(action_type);
        if (StringUtils.isBlank(code)){
            log.info("接收了收到夜雨的回调信息的回调信息当前流水号：{}，事件类型{}不合法，不做数据回传", requestId,action_type);
            return ResponseEntity.ok(new Result());
        }
        reportDTO.setResultType(code);
        reportDTO.setResultTime(time);
        reportDTO.setUpdateTime(new Date());
        reportService.updateData(reportDTO,action_type);
        //把数据回传给金袋
        String callback = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        log.info("dspId{},请求流水号{}收到夜雨的回调信息回传给账号{}的地址为：{}", reportDTO.getDspId(), requestId,reportDTO.getDspId(), callback);
        if (!callback.contains("conv_action=__EVENT__")) {
            log.info("dspId{},请求流水号{}收到夜雨的回调信息回传给聚告的地址不包含转化回传信息conv_action=__EVENT__ 不做回传", reportDTO.getDspId(), requestId);
            return ResponseEntity.ok(new Result());
        }
        String replaceCallback = callback.replace("__EVENT__", code);
        log.info("dspId{},请求流水号{}收到夜雨的回调信息回传给账号{}的最终地址为：{}", reportDTO.getDspId(), requestId,reportDTO.getDspId(), replaceCallback);
        String result = HttpUtil.get(replaceCallback);
        log.info("dspId{},请求流水号{}收到夜雨的回调信息回传给账号{}的响应结果：{}", reportDTO.getDspId(), reportDTO.getDspId(),requestId, result);
        return ResponseEntity.ok(new Result());
    }





    @GetMapping("/jd/ttyy/callback")
    public ResponseEntity jdTtyyCallBackUrl(String requestId, String conv_action) {
        log.info("接收了TT语音给金袋的回调信息请求流水号{},回调类型为{}", requestId, conv_action);
        if (StringUtils.isBlank(requestId) || StringUtils.isBlank(conv_action)) {
            log.info("接收了TT语音给金袋的回调信息请求参数异常,requestId{} event_type{}", requestId, conv_action);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", requestId);
        List<ReportDTO> list = reportService.list(params);
        String time = String.valueOf(System.currentTimeMillis());
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);
        } else {
            log.error("TT语音给金袋回传流水号{}，系统当前不存在", requestId);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        AccountDTO accountDTO = AccountLocalCache.getInstance().get(reportDTO.getDspId());
        String sspExtend = accountDTO.getSspExtend();
        JSONObject jsonObject = JSONObject.parseObject(sspExtend);
        String code = jsonObject.getString(conv_action);
        if (StringUtils.isBlank(code)){
            log.info("接收了TT语音给金袋的回调信息当前流水号：{}，事件类型{}不合法，不做数据回传", requestId,conv_action);
            return ResponseEntity.ok(new Result());
        }
        reportDTO.setResultType(code);
        reportDTO.setResultTime(time);
        reportDTO.setUpdateTime(new Date());
        reportService.updateData(reportDTO,conv_action);
        //把数据回传给深圳大白
        String callback = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        log.info("dspId{},请求流水号{}TT语音回传给金袋的地址为：{}", reportDTO.getDspId(), requestId, callback);
        if (!callback.contains("conv_action=__EVENT__")) {
            log.info("dspId{},请求流水号{}TT语音回传给金袋的地址不包含转化回传信息conv_action=__EVENT__ 不做回传", reportDTO.getDspId(), requestId);
            return ResponseEntity.ok(new Result());
        }
        String replaceCallback = callback.replace("__EVENT__", code);
        log.info("dspId{},请求流水号{}TT语音回传给金袋的最终地址为：{}", reportDTO.getDspId(), requestId, replaceCallback);
        String result = HttpUtil.get(replaceCallback);
        log.info("dspId{},请求流水号{}TT语音回传给金袋的响应结果：{}", reportDTO.getDspId(), requestId, result);
        return ResponseEntity.ok(new Result());
    }







    @GetMapping("/jd/xfxh/callback")
    public ResponseEntity jdXfxhCallBackUrl(String requestId, String conv_action) {
        log.info("接收了讯飞星火给金袋的回调信息请求流水号{},回调类型为{}", requestId, conv_action);
        if (StringUtils.isBlank(requestId) || StringUtils.isBlank(conv_action)) {
            log.info("接收了讯飞星火给金袋的回调信息请求参数异常,requestId{} event_type{}", requestId, conv_action);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("bizRequestId", requestId);
        List<ReportDTO> list = reportService.list(params);
        String time = String.valueOf(System.currentTimeMillis());
        ReportDTO reportDTO;
        if (CollectionUtil.isNotEmpty(list)) {
            reportDTO = list.get(0);
        } else {
            log.error("TT语音给金袋回传流水号{}，系统当前不存在", requestId);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        AccountDTO accountDTO = AccountLocalCache.getInstance().get(reportDTO.getDspId());
        String sspExtend = accountDTO.getSspExtend();
        JSONObject jsonObject = JSONObject.parseObject(sspExtend);
        String code = jsonObject.getString(conv_action);
        if (StringUtils.isBlank(code)){
            log.info("接收了讯飞星火给金袋的回调信息当前流水号：{}，事件类型{}不合法，不做数据回传", requestId,conv_action);
            return ResponseEntity.ok(new Result());
        }
        reportDTO.setResultType(code);
        reportDTO.setResultTime(time);
        reportDTO.setUpdateTime(new Date());
        reportService.updateData(reportDTO,conv_action);
        //把数据回传给深圳大白
        String callback = URLEncoderDecoderTool.decodeURL(reportDTO.getCallback());
        log.info("dspId{},请求流水号{}讯飞星火回传给金袋的地址为：{}", reportDTO.getDspId(), requestId, callback);
        if (!callback.contains("conv_action=__EVENT__")) {
            log.info("dspId{},请求流水号{}讯飞星火回传给金袋的地址不包含转化回传信息conv_action=__EVENT__ 不做回传", reportDTO.getDspId(), requestId);
            return ResponseEntity.ok(new Result());
        }
        String replaceCallback = callback.replace("__EVENT__", code);
        log.info("dspId{},请求流水号{}讯飞星火回传给金袋的最终地址为：{}", reportDTO.getDspId(), requestId, replaceCallback);
        String result = HttpUtil.get(replaceCallback);
        log.info("dspId{},请求流水号{}讯飞星火回传给金袋的响应结果：{}", reportDTO.getDspId(), requestId, result);
        return ResponseEntity.ok(new Result());
    }

}
